package com.jshongli.myapplication;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.jshongli.myapplication.base.BaseActivity;
import com.jshongli.myapplication.common.callback.MyCallback;
import com.jshongli.myapplication.common.utils.ToastUtils;
import com.jshongli.myapplication.common.utils.URLUtils;
import com.jshongli.myapplication.common.utils.UtilPreferences;
import com.jshongli.myapplication.login.LoginActivity;
import com.jshongli.myapplication.model.user.UserInfo;
import com.zhy.http.okhttp.OkHttpUtils;

import okhttp3.Call;

public class SplashActivity extends BaseActivity {

    @Override
    protected int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initView() {
        super.initView();
        mToolbar.setVisibility(View.GONE);

        //校验token
        checkToken();
    }

    public void checkToken(){
        Boolean hastoken = UtilPreferences.containsKey(mContext,"token");
        if(hastoken)
        {
            String token = UtilPreferences.getString(mContext,"token");
            if(token == "")
            {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        openActivity(LoginActivity.class,true);
                    }
                }, 2000);
            }else {
                OkHttpUtils.post().url(URLUtils.URLNoToken("manageAPI/auth/isLogin")).addHeader("Authorization",token).tag(this)
                        .build().execute(new MyCallback<UserInfo>(mContext) {

                    @Override
                    public void onSuccess(UserInfo response) {
                        if(response.success)
                        {
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                 openActivity(MainActivity.class, true);
                                }
                             }, 2000);

                        }else {
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    openActivity(LoginActivity.class, true);
                                }
                            }, 2000);
                        }
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        super.onError(call, e, id);
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                openActivity(LoginActivity.class, true);
                            }
                        }, 2000);
                    }

                    @Override
                    public void onFail(UserInfo response) {
                        super.onFail(response);
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                openActivity(LoginActivity.class, true);
                            }
                        }, 2000);
                    }
                });
            }
        }else {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    openActivity(LoginActivity.class,true);
                }
            }, 2000);
        }
    }
}